home *** CD-ROM | disk | FTP | other *** search
/ ETO Development Tools 2 / ETO Development Tools 2.iso / Tools - Objects / MacApp / MacApp CD Release / MacApp 2.0.1 (Many Libraries) / Interfaces / PInterfaces / UObject.p < prev    next >
Encoding:
Text File  |  1990-10-25  |  12.8 KB  |  295 lines  |  [TEXT/MPS ]

  1. {[a-,body+,h-,o=100,r+,rec+,t=4,u+,#+,j=20/57/1$,n-]}
  2. { UObject.p }
  3. { Copyright © 1984-1990  Apple Computer, Inc.  All rights reserved. }
  4.  
  5. {$IFC UNDEFINED UsingIncludes}
  6. {$SETC UsingIncludes := FALSE}
  7. {$ENDC}
  8.  
  9. {$IFC NOT UsingIncludes}
  10. UNIT UObject;
  11.  
  12.     INTERFACE
  13.         {$ENDC}
  14.  
  15.         {$IFC UNDEFINED __UObject__}
  16.         {$SETC __UObject__ := FALSE}
  17.         {$ENDC}
  18.  
  19.         {$IFC NOT __UObject__}
  20.         {$SETC __UObject__ := TRUE}
  21.  
  22.         { • Auto-Include the requirements for this unit's interface. }
  23.         {$SETC UObjectIncludes := UsingIncludes}
  24.         {$SETC UsingIncludes := TRUE}
  25.         {$I+}
  26.         {$IFC UNDEFINED __UMacAppUtilities__} {$I UMacAppUtilities.p} {$ENDC}
  27.         {$IFC UNDEFINED UsingMemory} {$I Memory.p} {$ENDC}
  28.         {$SETC UsingIncludes := UObjectIncludes}
  29.  
  30.         CONST
  31.             kFailNone            = 1;
  32.             kFailAbstract        = 2;
  33.             kFailCoercion        = 3;
  34.             kFailMethNotFound    = 4;
  35.  
  36.             kInvalidObj         = '*Not an object*';    { return value from LookupObjName if not an
  37.                                                          object}
  38.             kNilClass            = 0;                    { Value for superclass of the root object }
  39.  
  40.         TYPE
  41.  
  42.             ObjClassID            = kNilClass..16766;     { Object Class identifier. The first two
  43.                                                          bytes of each object contain the class of
  44.                                                          the object as an ObjClassID. }
  45.             ObjClassIDPtr        = ^ObjClassID;            { Preferred }
  46.             ObjClassIDHandle    = ^ObjClassIDPtr;        { Preferred }
  47.             PObjClassID         = ObjClassIDPtr;        { Left in for compatibility (2.0) }
  48.             HObjClassID         = ObjClassIDHandle;     { Left in for compatibility (2.0) }
  49.  
  50. {--------------------------------------------------------------------------------------------------}
  51.             { Definition of the system's root object }
  52.  
  53.  
  54.             TObject             = OBJECT
  55.                 FUNCTION TObject.Clone: TObject;
  56.                 { Makes a duplicate copy of SELF. The default calls SELF.ShallowClone, which makes a
  57.                 literal copy of instance variables but does not attempt to clone owned objects. A
  58.                 subclass which owns other objects should override this to clone the owned objects
  59.                 and data structures as well. }
  60.  
  61.                 PROCEDURE TObject.Fields(PROCEDURE DoToField(fieldName: Str255;
  62.                                                              fieldAddr: Ptr;
  63.                                                              fieldType: INTEGER));
  64.                 { Calls DoToField once for each instance variable in SELF. fieldName is a string
  65.                 representing the name of the variable. fieldAddr is the address of that variable.
  66.                 fieldType is one of the types defined in UMacAppUtilities. It is legal to use a
  67.                 local variable to hold data and pass the address of that variable to DoToField as
  68.                 if it were part of the object's instance variables. By convention, the first call
  69.                 to DoToField should have the class name as the fieldName, NIL as the fieldAddr, and
  70.                 bClass as the fieldType. Each subclass should override this method, particularly
  71.                 with qDebug on, dump its own information, then call INHERITED. }
  72.  
  73.                 PROCEDURE TObject.DynamicFields(PROCEDURE
  74.                                                            DoToField(fieldName: Str255;
  75.                                                                      fieldAddr: Ptr;
  76.                                                                      fieldType: integer));
  77.                 { Used by the Inspector and the Debugger to display the contents of this class's
  78.                 dynamic area. Defaults to nothing.  Override if your object uses the dynamic area
  79.                 See TList for an example. }
  80.  
  81.                 PROCEDURE TObject.ForAllSubClassesDo(PROCEDURE
  82.                                                      DoToSubClass(theClass: ObjClassID));
  83.                 { Calls DoToSubClass for each subclass of SELF. The order of traversal is arbitrary;
  84.                 see EachSubClass global procedure. }
  85.  
  86.                 PROCEDURE TObject.ForAllSuperClassesDo(PROCEDURE
  87.                                                        DoToSubClass(theClass: ObjClassID));
  88.                 { Calls DoToSubClass for each superclass of SELF. The order of traversal is the order
  89.                 of ancestry starting from the class of SELF; see EachSuperClass global procedure. }
  90.  
  91.                 PROCEDURE TObject.Free;
  92.                 { Called to dispose of an object. Gives object a chance to cleanup after itself.
  93.                 Default simply calls SELF.ShallowFree, which makes no attempt to free instance
  94.                 variables. Should be overridden by any class which allocates space or owns other
  95.                 objects in its instance variables.. Be sure to call INHERITED! }
  96.  
  97.                 FUNCTION TObject.GetClass: ObjClassID;
  98.                 { Returns the class ID of SELF. }
  99.  
  100.                 PROCEDURE TObject.GetClassName(VAR clName: MAName);
  101.                 { Returns the class name of SELF. }
  102.  
  103.                 FUNCTION TObject.GetClassSize: Size;
  104.                 { Returns the basic instantiation size. i.e. the size in bytes of a newly created
  105.                 object of this class }
  106.  
  107.                 FUNCTION TObject.GetDynamicPtr: Ptr;
  108.                 { Our objects have a dynamic area available. In 2.0 it is at the end of the object
  109.                 handle but it may not always be so.  Returns a direct heap pointer to the start
  110.                 of the dynamic area.  Use with caution as this pointer can be invalidated if the
  111.                 heap compacts. }
  112.  
  113.                 FUNCTION TObject.GetDynamicSize: Size;
  114.                 { Our objects can have a dynamic area that can be allocated with a SetDynamicSize
  115.                 call.  Returns the size of the dynamic area in bytes. Preferred. }
  116.  
  117.                 FUNCTION TObject.GetInstanceSize: Size;
  118.                 { Our objects have a dynamic area after all the fixed fields. Returns the current
  119.                 size in bytes of this instance accounting for the dynamic area.
  120.                 Left in for compatibility (2.0)  }
  121.  
  122.                 FUNCTION GetSuperClass: ObjClassID;
  123.                 { Returns immediate superclass of class. Returns kNilClass for TObject. If we ever
  124.                 get MI this will have to be an enumerator. }
  125.  
  126.                 PROCEDURE TObject.GetInspectorName(VAR inspectorName: Str255);
  127.                 { Returns additional information useful to the inspector. For instance (ha ha!) a
  128.                 TDocument could give its title. }
  129.  
  130.                 PROCEDURE TObject.Initialize;
  131.                 { Call this to put a newly created object into a known state. Subclasses should
  132.                 override it and call INHERITED. Typically used to put the object into a state where
  133.                 it can be safely FREED. Defaults to nothing. }
  134.  
  135.                 PROCEDURE TObject.Inspect;
  136.                 { Called from the debugger. Displays the fields of the object in the writeln window
  137.                 by calling the object's Fields method, passing InspectField as the DoToField
  138.                 routine. You can override this if you want to display any additional information in
  139.                 the writeln window when an object is inspected with the MacApp debugger. NOTE: this
  140.                 is not currently called from the "Inspector".}
  141.  
  142.                 PROCEDURE TObject.IObject;
  143.                 { The ancestral initializer. Should be called in the I<ClassName> chain. i.e. IView
  144.                 -> IEvtHandler -> IOBJECT }
  145.  
  146.                 FUNCTION TObject.IsMemberClass(testClass: ObjClassID): BOOLEAN;
  147.                 { True if I am a MEMBER of the testClass. i.e. the same class OR a subclass. }
  148.  
  149.                 FUNCTION TObject.IsSameClass(testClass: ObjClassID): BOOLEAN;
  150.                 { True if I belong to the immediate class. }
  151.  
  152.                 FUNCTION TObject.Lock(lockIt: BOOLEAN): BOOLEAN;
  153.                 { Locks down or unlocks an object AND its dynamic area if any. Returns old state.
  154.                 Since objects can float around in memory some occasions may arise where they need
  155.                 to be locked down (The Inspect method is an example: since addresses of instance
  156.                 variables are being passed, the object had better not move during the Inspect!) 
  157.                 Typically the old state is saved in a local and re-applied when ascending the
  158.                 call chain. Don't forget to re-apply the old state from failure handlers.
  159.                 _PLEASE_ use this sparingly and don't just go locking stuff down willy-nilly just
  160.                 because you might be afraid of handles. }
  161.  
  162.                 PROCEDURE TObject.SetDynamicSize(newSize: Size);
  163.                 { Set this instance's Dynamic Area size in bytes.  The dynamic area starts out
  164.                 unallocated. Signals Failure on memory error.  The size applies only to the size
  165.                 of the Dynamic Area. Preferred. }
  166.  
  167.                 PROCEDURE TObject.SetInstanceSize(newSize: Size);
  168.                 { Set this instance's size in bytes. Thus changing the size of the dynamic area.
  169.                 Fails on attempts to set a size smaller than instantiation size and on memory
  170.                 error.   Left in for compatibility (2.0) }
  171.  
  172.                 FUNCTION TObject.ShallowClone: TObject;
  173.                 { Lowest level method for copying an object; should not be overridden except in very
  174.                 unusual cases. Simply calls HandToHand to copy the object data. }
  175.  
  176.                 PROCEDURE TObject.ShallowFree;
  177.                 { Lowest level method for freeing an object; should not be overridden except in very
  178.                 unusual cases. Simply calls Dispose (the Pascal builtin) to free the object. }
  179.  
  180.                 END;
  181.  
  182.             TObjectPtr            = ^TObject;             { Preferred }
  183.             TObjectHandle        = ^TObjectPtr;            { Preferred }
  184.             PTObject            = TObjectPtr;            { Left in for compatibility (2.0) }
  185.             HTObject            = TObjectHandle;        { Left in for compatibility (2.0) }
  186.  
  187. {--------------------------------------------------------------------------------------------------}
  188.             { Utility routines to support objects }
  189.  
  190.         FUNCTION AddNewObjectsToInspector(add: BOOLEAN): BOOLEAN;
  191.         { Set to true (default) to automatically add all newly created objects to the inspector.
  192.         Function returns old setting. }
  193.  
  194.         FUNCTION AllocateObjectsFromPerm(allocateFromPerm: BOOLEAN): BOOLEAN;
  195.         { Set to TRUE to make object allocation calls use Permanent memory (the default).
  196.         Set to false to make object allocation calls use temporary memory (if they cannot be
  197.         allowed to fail).  Returns old state }
  198.  
  199.         FUNCTION DisciplineMethodCalls(discipline: BOOLEAN): BOOLEAN;
  200.         { Set to true to discipline all method calls that go through the dispatcher. Function returns
  201.         old setting. }
  202.  
  203.         PROCEDURE EachClassDo(PROCEDURE DoToClass(theClass: ObjClassID));
  204.         { Calls DoToClass for each class in the application }
  205.  
  206.         PROCEDURE EachSubClassDo(testClass: ObjClassID;
  207.                                  PROCEDURE DoToClass(theClass: ObjClassID));
  208.         { Calls DoToClass for subclass of the test class. Uses EachClassDo, so the order of iteration
  209.         is not based on the class hierarchy but on the sequence of class IDs in the class table,
  210.         Sorry… }
  211.  
  212.         PROCEDURE EachSuperClassDo(testClass: ObjClassID;
  213.                                    PROCEDURE DoToClass(theClass: ObjClassID));
  214.         { Calls DoToClass for superclass of the test class. Order of iteration is in order of
  215.         ancestry. }
  216.  
  217.         PROCEDURE FailNonObject(obj: UNIV TObject);
  218.         { called by dispatcher to discipline an object. Invokes failure if parameter is not IsObject
  219.         }
  220.  
  221.         PROCEDURE FreeIfObject(obj: TObject);
  222.         { IF obj <> NIL THEN obj.Free; useful for freeing an object that might sometimes be NIL. }
  223.  
  224.         PROCEDURE FreeObject(obj: TObject);
  225.         { Synonym for FreeIfObject. Left in for compatibility (2.0) }
  226.  
  227.         FUNCTION GetClassSizeFromID(classID: ObjClassID): Size;
  228.         { Given an object id, return the class's instantiation size. }
  229.  
  230.         PROCEDURE GetClassNameFromID(classID: ObjClassID;
  231.                                      VAR clName: MAName);
  232.         { Given an object id, return the object's name. ??? add error checking ??? }
  233.  
  234.         FUNCTION GetClassIDFromName(clName: MAName): ObjClassID;
  235.         { Given an object name, return its id. }
  236.  
  237.         FUNCTION GetClassID(obj: TObject): ObjClassID;
  238.         { Given an object, return the object's ClassID. }
  239.  
  240.         FUNCTION GetSuperClassID(objID: ObjClassID): ObjClassID;
  241.         { returns immediate superclass of class. Returns kNilClass for TObject. If we ever get MI
  242.         this will have to be an enumerator. }
  243.  
  244.         PROCEDURE IDUobject;
  245.         { Writeln UObject compile date }
  246.  
  247.         PROCEDURE InitUObject;
  248.         { Essential one time initialization for this unit }
  249.  
  250.         PROCEDURE InspectObject(obj: TObject);
  251.         { Called by debugger to inspect an object. NOTE: not currently called by the "inspector" }
  252.  
  253.         FUNCTION IsObject(obj: UNIV TObject): BOOLEAN;
  254.         { Debugging check. Returns true if obj references a real object, false if obj is NIL or a
  255.         non-object reference. This is not an absloutely sure-fire test of objectness, but does
  256.         validate the following: (1) is it a handle? (2) is the handle non-purgeable? (3) is the
  257.         class ID valid? (4) is the handle size at least as large as the class size? }
  258.  
  259.         FUNCTION IsClassIDMemberClass(testClass: ObjClassID;
  260.                                       superClass: ObjClassID): BOOLEAN;
  261.         { tests the testClass for MEMBERship in the superclass }
  262.  
  263.         FUNCTION IsMemberClassID(obj: TObject;
  264.                                  objID: ObjClassID): BOOLEAN;
  265.        { Returns true if obj references the class or a subclass of the class represented by objID. }
  266.  
  267.         PROCEDURE OBJFail(error: INTEGER);
  268.         { Entered due (presumably) to some object failing a test for objectness. There are two codes
  269.         which result in a special message: kFailCoercion, which means that an object could not be
  270.         coerced to a class due to not being a subclass of that class; and kFailMethNotFound, which
  271.         means a method was called for a class, but that method is not defined for the class. Other
  272.         codes are allowed but are not given special treatment. In qDebug mode a ProgramBreak
  273.         preceeds the Failure. }
  274.  
  275.         FUNCTION NewObjectByClassId(classID: ObjClassID): TObject;
  276.         { Creates an object of the given class id }
  277.  
  278.         FUNCTION NewObjectByClassName(className: MAName): TObject;
  279.         { Creates an object of the given class name }
  280.  
  281.         FUNCTION VerboseIsObject(obj: UNIV TObject): BOOLEAN;
  282.         { Debugging check. Indicate whether the parameter appears to really be an object and emit
  283.         diagnostics if it is not }
  284.  
  285.         PROCEDURE WrLblField(fieldName: Str255;
  286.                              fieldAddr: Ptr;
  287.                              fieldType: INTEGER);
  288.         { Writes the given field in the writeln window in the form: WRITE(fieldName, '=',
  289.         valueAsString).}
  290.         {$ENDC}
  291.  
  292.         {$IFC NOT UsingIncludes}
  293. END.
  294. {$ENDC}
  295.